<?php

namespace app\admin\controller;





use think\Cookie;
use think\Db;
use think\Request;
use think\Session;
use Verification\Verification;

class Index extends Verification
{
    /**
     * 后台登录页面
     * @return \think\response\View
     */
    public function index()
    {
        //判断有无cookie
        if (Cookie::has('LOL')){
            return view('admin@main/index');
        }else{
            //判断有无session
            if(Session::has('LOL','think')){
              $session= Session::get('LOL','think');

                Cookie::set('LOL', $session, 600);

                return view('admin@main/index');
            }else{
                return view('admin@index/login');
            }
        }
    }

    /**
     * 后台登录处理
     * @return \think\response\View
     */
    public function logindo()
    {
        $data=Request::instance()->param();

        $lol=DB::name('user')
            ->field(['name','pwd','id'])
            ->where('name',$data['name'])
            ->find();
        if ( $data['pwd']!==$lol['pwd']){
            return     $this->error('密码错误');
        }
//        dump( $data);

//            $list=Db::field('*')
//            ->table(['uj_role'=>'r','uj_user'=>'u', 'uj_user_role'=>'ur' ,'uj_role_jurisdiction'=>'rj','uj_jurisdiction'=>'j'])
//            ->where('u.id=ur.uid')
//            ->where('r.id=ur.rid')
//            ->where('r.id=rj.rid')
//            ->where('j.id=rj.jid')
//            ->where('j.id',8)
//            ->where('u.name',$data['name'])
//            ->find();
        $list=Db::field('r.id')
            ->table(['uj_role'=>'r','uj_user'=>'u', 'uj_user_role'=>'ur'])
            ->where('u.id=ur.uid')
            ->where('r.id=ur.rid')

            ->where('u.id',$lol['id'])
            ->select();

        if(empty($list)){
              return  $this->error('权限不足');
      }
            $data=[];
            foreach ($list as $k =>$v1){

                $list1=Db::field('j.m_name,j.c_name,j.f_name')
                    ->table(['uj_role_jurisdiction'=>'rj','uj_jurisdiction'=>'j','uj_role'=>'r'])
                    ->where('r.id=rj.rid')
                    ->where('j.id=rj.jid')
                    ->where('r.id',$v1['id'])
                    ->select();
                        for ($i=0;$i<count($list1);$i++ ){
                            if (in_array($list1[$i],$data)==false){

                           array_push($data,$list1[$i]);

                        }
                    }

//                var_dump($list1);die;

            }


        if(!empty($list1)){

            $str=[];

//            var_dump($list1);
//            dump($data);die;
            for($p=0; $p<count($data);$p++){

                $arr[1]=$data[$p]['m_name'];
                $arr[2]=$data[$p]['c_name'];
                $arr[3]=$data[$p]['f_name'];

                $str[$p]=implode ('/', $arr);
                $arr=null;
            }



            if ($this->yz($str)){
                    Session::set('LOL',[
                        'name'=>$lol['name'],
                        'pwd'=>$lol['pwd'],
                        'yz'=>$str
                    ],'think');
                    Cookie::set('LOL',[
                        'name'=>$lol['name'],
                        'pwd'=>$lol['pwd'],
                        'yz'=>$str
                    ]);
//                    var_dump(Session::get('LOL','think'));die;
                return $this->success('登入成功','admin/main/index');


                }




            }
                return  $this->error('权限不足');

    }

    /**
     * 后台登出处理
     * @return \think\response\View
     */
    public function logout()
    {

        Session::delete('LOL','think');

        Cookie::delete('LOL');


        $this->redirect('admin/index/index');
    }





}
